package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.mapper.AccountMapper;
import com.zhentao.pojo.Account;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 账户管理接口
 */
@RestController
@RequestMapping("/finance/account")
public class AccountController {

    @Autowired
    private AccountMapper accountMapper;

    /**
     * 分页查询账户
     */
    @GetMapping("/list")
    public Result<Page<Account>> list(
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            Account query) {
        Page<Account> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        
        if (query != null) {
            if (query.getIsDefault() != null) {
                wrapper.eq("is_default", query.getIsDefault());
            }
            if (StringUtils.hasText(query.getName())) {
                wrapper.like("name", query.getName());
            }
            if (StringUtils.hasText(query.getType())) {
                wrapper.like("type", query.getType());
            }
            if (query.getTenantId() != null) {
                wrapper.eq("tenant_id", query.getTenantId());
            }
        }
        wrapper.orderByDesc("id");
        
        return Result.success(accountMapper.selectPage(page, wrapper));
    }

    /**
     * 获取所有账户（不分页）
     */
    @GetMapping("/all")
    public Result<List<Account>> getAll(Account query) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        
        if (query != null) {
            if (query.getIsDefault() != null) {
                wrapper.eq("is_default", query.getIsDefault());
            }
            if (query.getTenantId() != null) {
                wrapper.eq("tenant_id", query.getTenantId());
            }
        }
        wrapper.orderByDesc("id");
        
        return Result.success(accountMapper.selectList(wrapper));
    }

    /**
     * 获取账户详情
     */
    @GetMapping("/detail/{id}")
    public Result<Account> getDetail(@PathVariable Long id) {
        return Result.success(accountMapper.selectById(id));
    }

    /**
     * 新增账户
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Account account) {
        if (account.getIsDefault() == null) {
            account.setIsDefault(0); // 0-非默认账户
        }
        if (account.getTenantId() == null) {
            account.setTenantId(1L);
        }
        if (account.getCurrentAmount() == null) {
            account.setCurrentAmount(account.getInitialAmount() != null ? account.getInitialAmount() : java.math.BigDecimal.ZERO);
        }
        accountMapper.insert(account);
        return Result.success();
    }

    /**
     * 修改账户
     */
    @PutMapping("/update")
    public Result<Void> update(@RequestBody Account account) {
        accountMapper.updateById(account);
        return Result.success();
    }

    /**
     * 删除账户
     */
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        accountMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除账户
     */
    @DeleteMapping("/batchDelete")
    public Result<Void> batchDelete(@RequestBody java.util.List<Long> ids) {
        accountMapper.deleteBatchIds(ids);
        return Result.success();
    }
}
